package com.yuantu.judiciary.utils;


import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Slf4j
public class PdfUtil {
    /**
     *  读取监听文件去水印
     *
     * @return boolean
     */
    public static boolean getLicense() {
        boolean result = false;
        try {
            //  license.xml应放在..\WebRoot\WEB-INF\classes路径下
            InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 生成pdf文档
     *
     * @param Address 地址
     */
    public static void doc2pdf(String Address) {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return;
        }
        try {
            //新建一个空白pdf文档
            long old = System.currentTimeMillis();
            File file = new File("E:\\\\石英工作\\\\EP-SERVER\\\\ep-service\\\\ep-swm\\\\ep-swm-service\\\\src\\\\main\\\\resources\\\\template\\\\SolidWasteApplyTemplate.pdf");
            FileOutputStream os = new FileOutputStream(file);
            //Address是将要被转化的word文档
            Document doc = new Document(Address);
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            doc.save(os, SaveFormat.PDF);
            long now = System.currentTimeMillis();
            //转化用时
            System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 把xwpfDocument转成pdf
     *
     * @param xwpfDocument xwpf文档
     * @param pdfPath      pdf
     */
    public static void docToPdf(XWPFDocument xwpfDocument, String pdfPath){
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return;
        }
        try {
            //新建一个空白pdf文档
            FileOutputStream os = new FileOutputStream(pdfPath);
            //Address是将要被转化的word文档
            //二进制OutputStream
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            //文档写入流
            xwpfDocument.write(baos);
            //OutputStream写入InputStream二进制流
            InputStream in = new ByteArrayInputStream(baos.toByteArray());
            Document doc = new Document(in);
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            doc.save(os, SaveFormat.PDF);
            log.info("导出成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void docToPdf(XWPFDocument xwpfDocument, OutputStream outputStream){
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return;
        }
        try {
            //二进制OutputStream
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            //文档写入流
            xwpfDocument.write(baos);
            //OutputStream写入InputStream二进制流
            InputStream in = new ByteArrayInputStream(baos.toByteArray());
            Document doc = new Document(in);
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            doc.save(outputStream, SaveFormat.PDF);
            log.info("导出成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
